/**
 * @Author: SevDaisy十七散人 
 * @Date: 2020-07-08 21:18:31
 */
package zyy.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import zyy.model.BeanAddress;
import zyy.util.BaseException;
import zyy.util.BusinessException;
import zyy.util.Bus;

public class FrmUserAddress extends JDialog implements ActionListener {
  private static final long serialVersionUID = -7010944395384654344L;

  private BeanAddress selectAddress = null;
  private Object tblAddressTitles[] = BeanAddress.userAddrTitle;
  private Object tblAddressData[][];
  DefaultTableModel tabAddressModel = new DefaultTableModel();
  private JTable dataTableAddress = new JTable(tabAddressModel);
  List<BeanAddress> allAddress = null;

  private JButton choose = new JButton("确认");
  private JButton add = new JButton("添加");
  private JButton unadd = new JButton("删除");
  private JPanel btns = new JPanel();

  @Override
  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == this.choose) {
      if (this.selectAddress == null) {
        JOptionPane.showMessageDialog(null, "请先选中一个地址", "错误", JOptionPane.ERROR_MESSAGE);
        this.selectAddress = this.allAddress.get(0);
        System.out.println("当前自动选中 0 位地址");
      }
      Bus.currentAddress = FrmUserAddress.this.selectAddress;
      // 更新订单信息，设定地址
      Bus.currentOrder.setuAddressID(Bus.currentAddress.getAddressID());
      System.out.println("确认地址是" + Bus.currentAddress);
      try {
        Bus.orderManager.sendToRider(Bus.currentOrder);
        Bus.fUserGo.setVisible(false);
      } catch (BusinessException be) {
        be.printStackTrace();
        JOptionPane.showMessageDialog(null, be.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
      }
      this.setVisible(false);
    } else if (e.getSource() == this.add) {
      // JDialog dlgaddAddr = new FrmAddrReg("用户");
      new FrmAddrReg("用户");
      // dlgaddAddr.setVisible(true);
      FrmUserAddress.this.reload();
    } else if (e.getSource() == this.unadd) {
      try {
        if (this.selectAddress == null) {
          JOptionPane.showMessageDialog(null, "请先选中一个地址", "错误", JOptionPane.ERROR_MESSAGE);
        }
        Bus.addrManager.unadd(this.selectAddress.getAddressID());
      } catch (Exception ex) {
        JOptionPane.showMessageDialog(null, ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
      }
    }
  }

  public void reload() {
    try {
      allAddress = Bus.addrManager.loadByUserID(Bus.currentUser.getUserID());
      // allAddress = Link.addrManager.loadByUserID(1);
    } catch (BaseException e) {
      JOptionPane.showMessageDialog(null, e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
      return;
    }
    tblAddressData = new Object[allAddress.size()][BeanAddress.userAddrTitle.length];
    for (int i = 0; i < allAddress.size(); i++) {
      for (int j = 0; j < BeanAddress.userAddrTitle.length; j++)
        tblAddressData[i][j] = allAddress.get(i).getCell(j);
    }
    tabAddressModel.setDataVector(tblAddressData, tblAddressTitles);
    this.dataTableAddress.validate();
    this.dataTableAddress.repaint();
  }

  public FrmUserAddress() {
    // Frm 基础设置
    {
      this.setTitle("外卖助手 用户 地址管理");
      // this.setLocationRelativeTo(null);
      this.setLocation(60, 45);
      this.setSize(815, 320);
      this.setVisible(true);
      Bus.fuUserAddress = this;
      this.reload();
    }
    // JTable 地址列表
    {
      this.getContentPane().setLayout(new BorderLayout());
      this.getContentPane().add(new JScrollPane(this.dataTableAddress), BorderLayout.CENTER);
      this.dataTableAddress.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
          int i = FrmUserAddress.this.dataTableAddress.getSelectedRow();
          if (i < 0) {
            return;
          }
          {
            // 同步数据更新和UI更新
            FrmUserAddress.this.selectAddress = FrmUserAddress.this.allAddress.get(i);
            System.out.println(FrmUserAddress.this.selectAddress);
            FrmUserAddress.this.reload();
            FrmUserAddress.this.dataTableAddress.setRowSelectionInterval(i, i);
          }
        }
      });
    }
    // btns 设置
    {
      btns.setLayout(new FlowLayout(FlowLayout.RIGHT));
      btns.add(choose);
      btns.add(add);
      btns.add(unadd);
      this.getContentPane().add(btns, BorderLayout.SOUTH);
      this.choose.addActionListener(this);
      this.add.addActionListener(this);
      this.unadd.addActionListener(this);
    }
  }

  // public static void main(String[] args) {
  // new FrmUserAddress();
  // }
}